package cn.wolfcode.shop.web.controller;

import cn.wolfcode.shop.domain.UserLogin;
import cn.wolfcode.shop.service.IProductService;
import cn.wolfcode.shop.service.IUserLoginService;
import cn.wolfcode.shop.vo.JsonResultVO;
import cn.wolfcode.shop.vo.ShopCarVo;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

@Controller
public class UserLoginController {
    @Reference
    private IUserLoginService userLoginService;
    @Reference
    private IProductService productService;

    /**
     * 注册接口
     * 移动端API接口
     * 注册接口
     * 地址：/user/register
     * 参数：String username ,String password
     * 请求方式：post
     * 响应吗：201
     * @param username
     * @param password
     * @param response
     * @return
     */
    @PostMapping("/user/register")
    @ResponseBody
    public JsonResultVO register(String username, String password, HttpServletResponse response){
        response.setStatus(HttpServletResponse.SC_CREATED);
        UserLogin userLogin = userLoginService.register(username, password);

        return JsonResultVO.success(userLogin);
    }

    /**
     * 登录接口
     * 地址：/user/login
     * 参数：String username ,String password
     * 请求方式：post
     * 响应吗：204
     * @param username
     * @param password
     * @param response
     * @return
     */
    @PostMapping("/user/login")
    @ResponseBody
    public JsonResultVO login(String username, String password, HttpServletResponse response){
        response.setStatus(HttpServletResponse.SC_OK);
        //返回token
        String token = userLoginService.login(username, password);
        return JsonResultVO.success(token);
    }

    /**
     * 注销接口
     * 地址：/user/loginout/{token}
     * 参数：String token
     * 请求方式：post
     * 响应吗：204
     * @param token
     * @return
     */
    @PostMapping("/user/loginout")
    @ResponseBody
    public JsonResultVO loginout(String token,HttpServletResponse response){
        //返回状态码
        response.setStatus(HttpServletResponse.SC_NO_CONTENT);
        userLoginService.loginout(token);
        return JsonResultVO.success(token);
    }

    /**
     * 添加购物车接口
     * 地址：/shopcar/add
     * 参数：String token,Long productSkuId, Integer number
     * 请求方式：post
     * 响应吗：201
     * @param response
     * @return
     */
    @PostMapping("/shopcar/add")
    @ResponseBody
    public JsonResultVO shopcar(String token,Long productSkuId, Integer number, HttpServletResponse response){
        //返回状态码
        response.setStatus(HttpServletResponse.SC_CREATED);

        productService.shopcar(token,productSkuId,number);
        return JsonResultVO.success();
    }





}
